/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.sys.util.common;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

/**
 *
 * @author Administrator
 */
public class ToolKit {

    /**
     * Returns the clientId for a component with id="foo"
     */
    public static String getClientId(FacesContext context, final String componentId) {
        UIComponent c = findComponent(context.getViewRoot(), componentId);
        String clientId = null;
        if (c != null) {
            clientId = c.getClientId(context);
        }
        return clientId;
    }

    /**
     * Finds component with the given id
     */
    private static UIComponent findComponent(UIComponent c, String id) {
        if (id.equals(c.getId())) {
            return c;
        }
        Iterator<UIComponent> kids = c.getFacetsAndChildren();
        while (kids.hasNext()) {
            UIComponent found = findComponent(kids.next(), id);
            if (found != null) {
                return found;
            }
        }
        return null;
    }

    public static void msgs_err(FacesContext context, String componentId, String newMessage) {
        if (context == null) {
            context = FacesContext.getCurrentInstance();
        }
        FacesMessage message = new FacesMessage(newMessage);
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        if (componentId == null || componentId.isEmpty()) {
            context.addMessage(null, message);
        } else {
            context.addMessage(getClientId(context, componentId), message);
        }
    }

    public static void msgs_err(String newMessage) {
        msgs_err(null, null, newMessage);

    }

    public static void msgs_info(FacesContext context, String componentId, String newMessage) {
        if (context == null) {
            context = FacesContext.getCurrentInstance();
        }
        FacesMessage message = new FacesMessage(newMessage);
        message.setSeverity(FacesMessage.SEVERITY_INFO);
        if (componentId == null || componentId.isEmpty()) {
            context.addMessage(null, message);
        } else {
            context.addMessage(getClientId(context, componentId), message);
        }
    }

    public static void msgs_info(String newMessage) {
        msgs_info(null, null, newMessage);
    }

    public static String dataRangeQueryParam(Date beginDate, Date endDate, String tableName) {
        if (tableName == null) {
            tableName = "";
        }
        if (!tableName.trim().isEmpty()) {
            tableName = tableName + ".";
        }

        if (beginDate == null || endDate == null) {
            return null;
        }
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginDate);
        int begin_year = begin.get(Calendar.YEAR);
        int begin_month = begin.get(Calendar.MONTH) + 1;

        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        int end_year = end.get(Calendar.YEAR);
        int end_month = end.get(Calendar.MONTH) + 1;

        if ((end_year - begin_year) > 10) {
            System.out.println("跨年不要超过10年！");
            ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "年份间隔不要超过10年！");
            return null;
        }
        String param;
        if (begin_year == end_year) {
            param = tableName + "iyear = " + begin_year + " and " + tableName + "imonth between " + begin_month + " and " + end_month;
        } else {

            String beginParam = "(" + tableName + "iyear =" + begin_year + " and " + tableName + "imonth > " + (begin_month - 1) + ")";

            String endParam = "(" + tableName + "iyear = " + end_year + " and " + tableName + "imonth < " + (end_month + 1) + ")";

            String middleParam = "";
            for (int i = begin_year + 1; i < end_year; i++) {
                middleParam += " or " + tableName + "iyear = " + i;
            }
            param = "(" + beginParam + middleParam + " or " + endParam + ")";
        }
        return param;
    }
}
